import os
import time

import yaml

from zwz.wyze.api.util import util
from zwz.zwzApi.api.util import generateNonce, generateTimestamp, generateSignature


class ReadYaml(object):
    def read_yml(self):
        # 获取配置文件路径
        curPath = os.path.abspath(os.path.dirname(__file__))
        yamlPath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + "datas/api_config.yml")
        # 将signature、nonce、timestamp，转换成正常数据
        f = open(yamlPath, 'r', encoding='utf-8')

        content = f.read()
        contents = yaml.load(content, Loader=yaml.FullLoader)
        '''
        print("文件中所有内容：", contents)
        print("登录接口内容：", contents['login'])
        '''
        # print(contents)
        return contents

    def get_url(self, api_name):
        content = self.read_yml()
        new_url = content['host'] + content[api_name]['url']
        # print("请求地址： ", new_url)
        return new_url

    def get_method(self, api_name):
        content = self.read_yml()
        # print("请求方式： ", content[api_name]['method'])
        return content[api_name]['method']

    def get_data(self, api_name):
        content = self.read_yml()
        # print("请求数据： ", content[api_name]['data'])
        return content[api_name]['data']

    def get_headers(self, api_name):
        # 获取yaml文件内容
        content = self.read_yml()

        met = content[api_name]['method']
        url = content[api_name]['url']
        # if content[api_name]['data']:
        data = content[api_name]['data']
        # else:
        #     data = {}
        # print(str(met).upper())
        # print(url)
        a = util()
        # if content[api_name]['headers']['X-Ca-Signature'] == None:
        content[api_name]['headers']['X-Ca-Signature'] = a.sign(secret="098ff21c253241bf9ac9e2761782a318",
                                                                method=met,
                                                                path=url,
                                                                headers={"X-Ca-Key": "5c092a8201474bb0",
                                                                         "Accept": "application/json",
                                                                         "X-Ca-Signature": "",
                                                                         "Content-Type": "application/json"},
                                                                querys=data, bodys={},
                                                                signHeaderPrefixList=[1])
        return content[api_name]['headers']

    # 获取响应结果的预期结果
    def get_expected(self, api_name):
        content = self.read_yml()
        expected = content[api_name]['expected']
        # print(expected, type(expected), expected["success"], expected["decimal"])
        return expected

    def get_time(self):
        # 以毫秒为单位的时间戳
        get_now_milli_time = int(time.time() * 1000)
        print(get_now_milli_time)


if __name__ == "__main__":
    # read_yml()
    ry = ReadYaml()
    # ry.is_login("login_prod")
    print(ry.get_headers('generate'))
    # print(ry.get_headers('detail'))
